import time

from fastapi import Request

from loguru import logger


async def compute_process_time(request: Request, call_next):
    start_time = time.monotonic()
    response = await call_next(request)
    process_time = time.monotonic() - start_time
    response.headers["X-Process-Time"] = str(process_time)
    logger.info(f"{request.method} {request['path']} cost {process_time:.2f}s")
    return response
